package com.tckdq.tcc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.tckdq.tcc.domain.CourseChapter;
import com.tckdq.tcc.mapper.CourseChapterMapper;
import com.tckdq.tcc.service.ICourseChapterService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程章节 ， 一个课程，多个章节，一个章节，多个视频 服务实现类
 * </p>
 *
 * @author whale
 * @since 2022-08-16
 */
@Service
public class CourseChapterServiceImpl extends ServiceImpl<CourseChapterMapper, CourseChapter> implements ICourseChapterService {

    @Autowired
    private CourseChapterMapper courseChapterMapper;

    @Override
    public boolean insert(CourseChapter entity) {
        EntityWrapper<CourseChapter> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("course_id",entity.getCourseId());
        List<CourseChapter> courseChaptersList = courseChapterMapper.selectList(entityWrapper);
        Integer max =0;
        List<Integer> collect = courseChaptersList.stream().map(c -> c.getNumber()).collect(Collectors.toList());
        Integer max1 = Collections.max(collect);
        max = max1+1;
        entity.setNumber(max);
        return super.insert(entity);
    }
}
